package cn.detu.ln.controller;

import cn.detu.ln.entity.Selection;
import cn.detu.ln.service.SelectionService;
import cn.detu.ln.utils.PageUtil;
import cn.detu.ln.utils.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("selection")
public class SelectionController {
    // 由于controller层需要调用service层方法，因此需要自动装配一个UserService对象
    @Autowired
    private SelectionService selectionService;
    @GetMapping("{url}")
    public RestUtil userGet(@PathVariable String url, Selection selection, PageUtil pageUtil, String search){
        switch (url){
            case "selectAll": return selectionService.selectAll();
            case "selectionDelete":return selectionService.selectionDelete(selection.getSelId());
            // 课程名称+学号
            case "querySelectionByPage":return selectionService.querySelectionByPage(pageUtil.getCurrent(),
                    pageUtil.getRows(),search);
            // 按照课程性质实现模糊查询
            case "querySelectionByCourseNaturePage":
                return selectionService.querySelectionByCourseNaturePage(pageUtil.getCurrent(),pageUtil.getRows(),
                        search);
            // 按课程性质模糊查询 + 学号
            case "querySelectionByCouNaturePage":return selectionService.querySelCourseByPage(pageUtil.getCurrent(),
                    pageUtil.getRows(),selection.getStuNum(),
                    search);
            // 课程id +学号
            case "querySelectionByIdPage":return selectionService.querySelCourseByIdPage(pageUtil.getCurrent(),
                    pageUtil.getRows(),selection.getStuNum(),
                    search);
            // 根据学号查询当前选课记录
            case "querySelectionByStuNumPage":return selectionService.querySelectionByStuNumPage(pageUtil.getCurrent(),
                    pageUtil.getRows(),
                    search);
            // 教师根据课程号查看学生选课记录
            case "querySelectionByCouIdPage":return selectionService.querySelectionByCouIdPage(pageUtil.getCurrent(),
                    pageUtil.getRows(),selection.getCouId());
            // 根据couId判断学生是否已经选过该课程
            case "selectSelectionByCouId":return selectionService.selectSelectionByCouId(selection.getCouId());
            // 通过couId查询当前选课selId
            case "selectSelIdByCouId":return selectionService.selectSelIdByCouId(selection.getCouId());
            // 通过couId查看当前课程是否被学生选择
            case "judgeSelectionByCouId":return  selectionService.judgeSelectionByCouId(selection.getCouId());
        }
        return null;
    }
    @PostMapping("{url}")
    public RestUtil userPost(@PathVariable String url, Selection selection){
        switch (url){
            case "selectionUpdate":return selectionService.selectionUpdate(selection);
            case "selectionInsert":return selectionService.selectionInsert(selection);
        }
        return null;
    }
    // GET请求根据selId获取单条数据
    // localhost:1320/selection/querySelectionById/90101
    @GetMapping("querySelectionById/{selId}")
    public Selection querySelectionById(@PathVariable("selId") Integer selId){
        return selectionService.querySelectionById(selId);
    }
    @GetMapping("querySelectionByStuNum/{stuNum}")
    public Selection querySelectionByStuId(@PathVariable("stuNum") Long stuNum){
        return selectionService.querySelectionByStuId(stuNum);
    }

}
